package com.loan.psychicloan.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.loan.psychicloan.R;
import com.loan.psychicloan.Util.MyCountDownTimer;
import com.loan.psychicloan.Util.OkHttpUtils;
import com.loan.psychicloan.Util.ValidateUtil;
import com.loan.psychicloan.bean.SendMSMBean;
import com.orhanobut.logger.Logger;

import static com.loan.psychicloan.R.color.bule_line;
import static com.loan.psychicloan.R.color.white;

/**
 * Created by watilion on 2017/12/31 0031.
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    Button btn_msm_login;
    Button btn_psw_login;
    EditText edit_login_username;
    EditText edit_login_msm;
    Button btn_login_sendmsm;
    Button btn_login;
    Button btn_forget;
    Button btn_register;
    private static int edit_login_msm_width ;
    public static String sendmsm_url = "https://www.psychicloan.com:8443/loan-api/loanuser/sendsms";
    public static String validataNumUrl = "https://www.psychicloan.com:8443/loan-api/loanuser/validataNum";
    private SendMSMBean mSendMSMBean = new SendMSMBean() ;
    private SendMSMBean validataNumBean =new SendMSMBean();
    private static boolean psw_login = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar mToolbar = findViewById(R.id.login_toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mIntent =new Intent(getApplicationContext(),MainActivity.class);
                startActivity(mIntent);
                finish();
            }
        });
        TextView tv_back_toolbar_title = findViewById(R.id.back_toolbar_title);
        tv_back_toolbar_title.setText("登录");

        initview();
    }


    private void initview() {
        btn_msm_login= findViewById(R.id.btn_msm_login);
        btn_psw_login = findViewById(R.id.btn_psw_login);
        edit_login_username = findViewById(R.id.edit_login_username);
        edit_login_msm = findViewById(R.id.edit_login_msm);
        edit_login_msm_width=edit_login_msm.getLayoutParams().width;
        btn_login_sendmsm = findViewById(R.id.btn_login_sendmsm);
        btn_login = findViewById(R.id.btn_login);
        btn_forget = findViewById(R.id.btn_forget);
        btn_register = findViewById(R.id.btn_register);


        btn_msm_login.setOnClickListener(this);
        btn_psw_login.setOnClickListener(this);
        btn_login_sendmsm.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        btn_forget.setOnClickListener(this);
        btn_register.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        ViewGroup.LayoutParams lp =edit_login_msm.getLayoutParams();
        switch (view.getId()){

            case R.id.btn_msm_login:
                psw_login = false;
                btn_psw_login.setBackground(getDrawable(R.drawable.btn_psw_unselect));
                btn_psw_login.setTextColor(this.getResources().getColor(white));
                edit_login_msm.setHint("验证码");
                edit_login_msm.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                lp.width = edit_login_msm_width;
                btn_msm_login.setBackground(getDrawable(R.drawable.btn_msm_select));
                btn_msm_login.setTextColor(this.getResources().getColor(bule_line));
                btn_login_sendmsm.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_psw_login:
                psw_login = true;
                btn_psw_login.setBackground(getDrawable(R.drawable.btn_psw_select));
                btn_psw_login.setTextColor(this.getResources().getColor(bule_line));
                edit_login_msm.setHint("密码");
                lp.width = edit_login_msm_width/3*5;
                edit_login_msm.setLayoutParams(lp);
                edit_login_msm.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                edit_login_msm.setText(null);
                btn_msm_login.setBackground(getDrawable(R.drawable.btn_msm_unselect));
                btn_msm_login.setTextColor(this.getResources().getColor(white));
                btn_login_sendmsm.setVisibility(View.GONE);
                break;
            case R.id.btn_login_sendmsm:
                String Phonenumber = edit_login_username.getText().toString();
                if (!ValidateUtil.validatePhoneNumber(Phonenumber)){
                    Toast.makeText(this,"手机号不正确，请检查手机号",Toast.LENGTH_SHORT).show();
                }else {
                    JsonObject jsonObject = new JsonObject();
                    //jsonObject.notifyAll();
                    //jsonObject.remove("");
                    jsonObject.addProperty("phonenumber", Phonenumber);
                    jsonObject.addProperty("type","2");
                    startSendMSMTask(sendmsm_url,jsonObject.toString());

                }
                break;
            case R.id.btn_login:
                if (TextUtils.isEmpty(edit_login_username.getText())){
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    if (!psw_login) {
                        if (TextUtils.isEmpty(edit_login_msm.getText())) {
                            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                        } else {
                            JsonObject mLogin_JsonObject = new JsonObject();
                            mLogin_JsonObject.addProperty("phonenumber", edit_login_username.getText().toString());
                            mLogin_JsonObject.addProperty("type", "2");
                            mLogin_JsonObject.addProperty("tamp", mSendMSMBean.data.tamp);
                            mLogin_JsonObject.addProperty("hashcode", mSendMSMBean.data.hashcode);
                            mLogin_JsonObject.addProperty("code", edit_login_msm.getText().toString());
                            startLoginTask(validataNumUrl, mLogin_JsonObject.toString());

                            //Toast.makeText(this,"登录",Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        if (!ValidateUtil.validatePhoneNumber(edit_login_username.getText().toString())) {
                            Toast.makeText(this,"手机号不正确，请检查手机号",Toast.LENGTH_SHORT).show();
                        } else {
                            if (TextUtils.isEmpty(edit_login_msm.getText())) {
                                Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                            } else {
                                String mLoginByPswURL = "https://www.psychicloan.com:8443/loan-api/loanuser/loginbypassword";
                                JsonObject mPswLogin_JsonObject = new JsonObject();
                                mPswLogin_JsonObject.addProperty("phonenumber", edit_login_username.getText().toString());
                                mPswLogin_JsonObject.addProperty("password", edit_login_msm.getText().toString());
                                Logger.json(mPswLogin_JsonObject.toString());
                                startLoginTask(mLoginByPswURL, mPswLogin_JsonObject.toString());
                            }
                        }
                    }
                }
                break;
            case R.id.btn_forget:
                //Toast.makeText(this,"忘记密码",Toast.LENGTH_SHORT).show();
                Intent mIntent2 = new Intent(this,ForgetActivity.class);
                startActivity(mIntent2);
//                finish();
                break;

            case R.id.btn_register:
                Intent mIntent = new Intent(this,RegisterActivity.class);
                startActivity(mIntent);
                //finish();
                break;
                default:
                    break;
        }
    }

    private void startLoginTask(String validataNumUrl, String json) {

        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        OkHttpUtils.getInstance().postByJson(this, validataNumUrl,json, SendMSMBean.class,
                new OkHttpUtils.CallBack <SendMSMBean>() {
                    @Override
                    public void getData(SendMSMBean sendMSMBean) {
                        if (sendMSMBean != null){
                            //如果不为空就用mLoanList接收
                            validataNumBean = sendMSMBean;
                            Toast.makeText(getApplicationContext(),validataNumBean.msg,Toast.LENGTH_SHORT).show();
                            saveLogin(validataNumBean);
                        }
                    }
                });
    }

    private void saveLogin(SendMSMBean mValidataNumBean) {
        if (mValidataNumBean.resultcode ==4 || mValidataNumBean.resultcode == 13){
            MainActivity.LOGIN = true;
            MainActivity.PHONE_NUMBER = edit_login_username.getText().toString();
            SharedPreferences sp = getSharedPreferences("config",0);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("PHONE_NUMBER",edit_login_username.getText().toString());
            editor.putBoolean("LOGIN",true);
            editor.apply();
            Intent mIntent = new Intent(this, MainActivity.class);
            startActivity(mIntent);
            //finish();

        }
    }

    public void startSendMSMTask(String mUrl, String json) {
        //通过类名直接调用静态方法获取单例对象再调用getBeanOfOK()方法传入参数通过接口回调获取数据
        OkHttpUtils.getInstance().postByJson(this, mUrl,json, SendMSMBean.class,
                new OkHttpUtils.CallBack <SendMSMBean>() {
                    @Override
                    public void getData(SendMSMBean sendMSMBean) {
                        if (sendMSMBean != null){
                            //如果不为空就用mLoanList接收
                            mSendMSMBean = sendMSMBean;
                            Toast.makeText(getApplicationContext(),mSendMSMBean.msg,Toast.LENGTH_SHORT).show();
                            setCountDownTimer(mSendMSMBean);
                        }
                    }
                });
    }

    public void setCountDownTimer(SendMSMBean mSendMSMBean) {
        if (mSendMSMBean.resultcode == 2){
            final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(btn_login_sendmsm,60000,1000);
            myCountDownTimer.start();
        }
    }
}
